package com.siwei.mes.handler;

import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.handler.service.ExperimentFactoryService;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账抽象类
 *
 * @Description: 试验台账动态factory  1-水泥 2-粉煤灰 3-矿渣粉 4-粗骨料 5-细骨料 6-外加剂 7-混凝土
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/2/21 22:03
 */
@Service
public class ExperimentFactory {
    @Resource
    private Map<String, ExperimentFactoryService> dynamicServiceMap = new ConcurrentHashMap<>();
    @Resource
    private ExperimentMapper experimentMapper;

    public synchronized ExperimentFactoryService getInstance(String type) {
        ExperimentFactoryService factoryService = dynamicServiceMap.get(type);
        assertTrue(factoryService == null, "对应试验类型为空");
        return factoryService;
    }

    public synchronized ExperimentFactoryService getInstanceByExperimentDetail(ExperimentDetail experimentDetail) {
        Experiment experiment = experimentMapper.loadById(experimentDetail.getExperimentId());
        assertTrue(experiment == null, "试验台账为空");
        ExperimentFactoryService factoryService = dynamicServiceMap.get(experiment.getExperimentType().toString());
        assertTrue(factoryService == null, "对应试验类型为空");
        return factoryService;
    }
}
